<template>
  <div>
    <el-table
      ref="table"
      :key="tableKey"
      v-loading="listLoading"
      :header-cell-style="{background:'#F5F4F7'}"
      :data="list"
      border
      fit
      stripe
      highlight-current-row
      :height="tableHeight"
      style="width: 100%;"
    >
      <el-table-column label="序号" min-width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="单据模板名称" min-width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.key }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="流程名称" width="220">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="描述" width="220">
        <template slot-scope="scope">
          <span>{{ scope.row.metaInfo.description }}</span>
        </template>
      </el-table-column>
      <el-table-column label="流程版本" min-width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.version }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" min-width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="150" class-name="mb0">
        <template slot-scope="{row}">
          <el-button type="text" @click="handleUpdate(row)">编辑</el-button>
          <el-button type="text" @click="sendUpdate(row)">发布</el-button>
          <el-button type="text" class="del" @click="handleModifyStatus(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      v-if="paging.total > 0"
      background
      :current-page.sync="paging.page"
      :page-sizes="[10, 20, 30, 40]"
      :page-size.sync="paging.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="paging.total"
      @size-change="queryListByPage('search')"
      @current-change="queryListByPage"
    />
    <DialogIframe
      ref="DialogIframe"
      :url-path="urlPath"
      @close="closeEvent"
    />
  </div>
</template>
<script>
import { queryList, del, open } from '@/api/system/approval-flow/approval-model'
import calculateCommonTableHeight from '@/layout/mixin/calculateCommonTableHeight'
import DialogIframe from '@/components/DialogIframe'
import { parseTime } from '@/utils'
export default {
  name: 'TableArea',
  components: { DialogIframe },
  mixins: [calculateCommonTableHeight],
  data() {
    return {
      list: [],
      tableKey: 0,
      listLoading: false,
      paging: {
        total: 0,
        page: 1,
        size: 10
      },
      formInline: {
        name: '',
        key: ''
      },
      urlPath: ''
    }
  },
  created() {
    this.queryListByPage()
  },
  methods: {
    queryListByPage(str = '') {
      if (str === 'search') {
        this.paging.page = 1
      }
      queryList(
        this.formInline,
        this.paging.page,
        this.paging.size
      ).then(res => {
        if (res.code === 0) {
          this.paging.total = res.count
          res.data.forEach(val => {
            val.metaInfo = JSON.parse(val.metaInfo)
          })
          this.list = res.data
          if (res.data.length) {
            const row = res.data[0]
            if (row.name === '新建流程' || row.metaInfo.description === '') {
              del(Number(row.id)).then(res => {
                if (res.flag === true) {
                  this.queryListByPage()
                }
              })
            }
          }
        }
      })
    },
    // 删除
    handleModifyStatus(row) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(Number(row.id)).then(res => {
          if (res.flag === true) {
            this.$message({
              showClose: true,
              message: '删除成功',
              type: 'success'
            })
            this.queryListByPage()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 发布
    sendUpdate(row) {
      open(Number(row.id)).then(res => {
        if (res.flag) {
          this.$message({
            showClose: true,
            message: '发布成功',
            type: 'success'
          })
          this.queryListByPage()
        }
      })
    },
    // 编辑
    handleUpdate(row) {
      this.$refs.DialogIframe.show()
      this.$refs.DialogIframe.width = window.innerWidth + 'px'
      this.$refs.DialogIframe.height = window.innerHeight - 100 + 'px'
      this.$refs.DialogIframe.top = '0px'
      this.urlPath = `${this.baseurl}:30006/static/modeler.html?modelId=` + row.id + '&lngproductid=315'
    },
    parseTime(time) {
      return parseTime(time)
    },
    closeEvent() {
      this.queryListByPage()
    }
  }
}
</script>

